package com.lin.reggie.controller;

import com.lin.reggie.common.R;
import com.lin.reggie.entity.Category;
import com.lin.reggie.entity.Page;
import com.lin.reggie.service.CateoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired(required = false)
    private CateoryService cateoryService;
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //输出数据给浏览器，浏览器采用附件下载接收数据，附件名字为category.xlsx
        response.setHeader("content-disposition","attachment;filename=category.xlsx");

        //1. 读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");

        //2。 使用模板的输入流构建一个工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);

        //3. 得到工作单
        Sheet sheet = workbook.getSheetAt(0);

        //4. 读取第一行,提取第一行的样式，并且存储到集合中
        Row row = sheet.getRow(1);
        List<CellStyle> cellStylesList= new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            //得到单元格的样式
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStylesList.add(cellStyle);
        }

        //5. 得到数据,遍历数据，把数据写入到excel
        List<Category> categoryList = cateoryService.findAll();
        for (int i = 0; i < categoryList.size(); i++) {
            //一个类别就对应一行
            row =  sheet.createRow(i+1);
            Category category = categoryList.get(i);
            //把类别信息设置到单元格上

            //类别类型
            Cell cell = row.createCell(0);
            //设置单元格内容
            cell.setCellValue(category.getType());
            cell.setCellStyle(cellStylesList.get(0));

            //类别名字
            cell = row.createCell(1);
            //设置单元格内容
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStylesList.get(1));


            //类别排序
            cell = row.createCell(2);
            //设置单元格内容
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStylesList.get(2));

            //创建时间
            cell = row.createCell(3);
            //设置单元格内容
            cell.setCellValue(category.getCreateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(3));


            //创建时间
            cell = row.createCell(4);
            //设置单元格内容
            cell.setCellValue(category.getUpdateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(4));
        }
        //6. 把工作薄写出
        workbook.write(response.getOutputStream());
    }

    //添加菜品分类
    @PostMapping
    public R<String>save(@RequestBody Category category, HttpSession session){
        //获取当前操作人id
        Long empId = (Long) session.getAttribute("employee");
        //设置创建人
        category.setCreateUser(empId);
        //设置修改人
        category.setUpdateUser(empId);
        cateoryService.save(category);
        return R.success("添加类别成功");
    }
    //查询菜品分类数据
    @GetMapping("/page")
    public  R<Page<Category>> findAll (Integer page, Integer pageSize){
        Page<Category> pageResult = cateoryService.findByPage(page, pageSize);
        return R.success(pageResult);
    }
    //删除菜品分类数据
    @DeleteMapping()
    public R<String>delete(Long id){
        cateoryService.removeById(id);
        return R.success("分类信息删除成功");
    }

    //修改菜品分类
    @PutMapping
    public R<String>update(@RequestBody Category category,HttpSession session){
        //获取登录员工
        Long category1 = (Long) session.getAttribute("employee");
        //完善信息（修改人）
        category.setUpdateUser(category1);
        //调用业务层修改
        cateoryService.update(category);
        //返回修改成功信息
        return R.success("修改成功");
    }
    //处理获取指定类别的分类列表数据请求
    @RequestMapping("/list")
    public R<List<Category>>list(Integer type){
        //调用业务获取指定类别分类别表数据
        List<Category> allByType = cateoryService.findAllByType(type);
        //封装数据返回
        return R.success(allByType);
    }
}
